import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home' // 重定向
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/HomeView.vue'),
      meta: {
        isshow: true
      }
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('@/views/SearchView.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('@/views/CartView.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
      meta: {
        isshow: true
      }
    },
    {
      path: '/xq',
      name: 'xq',
      component: () => import('@/views/Xiangqing.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('@/views/UserView.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/dizhi',
      name: 'dizhi',
      component: () => import('@/views/dizhi.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/dingdan',
      name: 'dingdan',
      component: () => import('@/views/dingdan.vue'),
      meta: {
        isshow: false
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/register/register.vue'),
      meta: {
        isshow: false
      },
      children: [
        {
          path: 'zhuce',
          name: 'zhece',
          component: () => import('@/views/register/zhuce.vue'),
          meta: {
            isshow: false
          }
        },
        {
          path: 'yzm',
          name: 'yzm',
          component: () => import('@/views/register/yzm.vue'),
          meta: {
            isshow: false
          }
        },
        {
          path: 'mm',
          name: 'mm',
          component: () => import('@/views/register/mima.vue'),
          meta: {
            isshow: false
          }
        },
      ]
    },
    { path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('@/views/NotFoundView.vue') },

  ]
})
  //   // 路由前置守卫
  //   router.beforeEach((to, from) => {
  //     // 有token就表示已经登录
  //     // 想要进入购物车页面，必须有登录标识token
  //     // console.log('to:', to)
  //     // console.log('from:', from)
  //     let token = localStorage.getItem('token')
  //     if (to.path == '/cart') {
  //         // 此时必须要有token
  //         if (token) {
  //           to.path == '/cart'
  //         } else {
  //             // Vue.prototype.$toast();
  //             alert('请先登录')
  //                 // 定时器
  //             setTimeout(() => {
  //                 to.path=="/login"; // 强制去到"/user"所对应的路由界面
  //             }, 1000);
  //         }
  //     } else {
  //         // 如果不是去往购物车的路由，则直接通过守卫，去到to所对应的路由界面
  //         to.path == ('')
  //     }
  // })
  // 设置全局前置守卫
router.beforeEach((to, from) => {
  if (to.path == '/cart') {
      if (!localStorage.getItem('token')) {
          // 表示本地没有token,获取的值为null
          // ElMessage({
          //     message: '用户未登录,请先登录!!!',
          //     type: 'warning',
          //     duration: 2000,
          // })
              alert('用户未登录,请先登录!')


          return { path: '/login' }
      }
  } 
  else if(to.path == '/login') {
      // 进入登录页
      if (localStorage.getItem('token')) {
          // 判断有没有token,有的话,友好提示
          // ElMessage({
          //     message: '登录状态有效,不需要重复登录!!!',
          //     type: 'warning',
          //     duration: 2000,
          // })
              alert('登录状态有效,不需要重复登录')

          return {
              path: '/home'
          }
      }

  }

})

export default router
